package com.trimps.wifi.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.trimps.wifi.mapper.DevCfgMapper;
import com.trimps.wifi.mapper.DevMapper;
import com.trimps.wifi.pojo.Device;
import com.trimps.wifi.pojo.DeviceCfg;
import com.trimps.wifi.service.IDevService;

@Service("devService")
public class DevServiceImpl implements IDevService {
    private static final Logger logger = Logger.getLogger(DevServiceImpl.class);
    
    private Map<String, Boolean> devCfg = new HashMap<String, Boolean>();
    
	@Autowired
	private DevMapper devMapper;	
	@Autowired
	private DevCfgMapper devCfgMapper;	

	@Override
	public int getDevCount() {
		return devMapper.getDevCount();
	}

	@Override
	public List<Device> getAllDevice(int currentPage, int rowPerPage) {
		return devMapper.getAllDevice(currentPage, rowPerPage);
	}
	
	@Override
	public void editDevCfg(DeviceCfg cfg) {
		DeviceCfg tmp = devCfgMapper.getDevCfgByDevNo(cfg.getDevNo());
		if(tmp != null) {
			devCfgMapper.update(cfg);
		}else{
			devCfgMapper.insert(cfg);
		}
		devCfg.put(cfg.getDevNo(), true);
	}

	@Override
	public Device getDevById(int id) {
		return devMapper.getDevById(id);
	}

	@Override
	public DeviceCfg getDevCfgByDevNo(String devNo) {
		return devCfgMapper.getDevCfgByDevNo(devNo);
	}

	@Override
	public int saveDevice(Device dev) {
		return devMapper.insert(dev);
	}
	
	@Override
	public int saveDeviceCfg(DeviceCfg cfg) {
		return devCfgMapper.insert(cfg);
	}

	@Override
	public void delByNo(String devNo) {
		devMapper.delByNo(devNo);
		devCfgMapper.delByDevNo(devNo);
	}

	@Override
	public Device getByDevNo(String devNo) {
		return devMapper.getByDevNo(devNo);
	}

}
